package org.sbbs.base.service.impl;

import java.io.Serializable;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.sbbs.base.dao.BaseDao;
import org.sbbs.base.pagequery.PagedQueryDao;
import org.sbbs.base.pagequery.hibernate.CustomCriteria;
import org.sbbs.base.pager.IPager;
import org.sbbs.base.searcher.SearchFilter;
import org.sbbs.base.service.BaseManager;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * This class serves as the Base class for all other Managers - namely to hold common CRUD methods that they might all
 * use. You should only need to extend this class when your require custom CRUD logic.
 * <p/>
 * <p>
 * To register this class in your Spring context file, use the following XML.
 * 
 * <pre>
 *     &lt;bean id="userManager" class="org.sbbs.service.impl.GenericManagerImpl"&gt;
 *         &lt;constructor-arg&gt;
 *             &lt;bean class="org.sbbs.dao.hibernate.GenericDaoHibernate"&gt;
 *                 &lt;constructor-arg value="org.sbbs.model.User"/&gt;
 *                 &lt;property name="sessionFactory" ref="sessionFactory"/&gt;
 *             &lt;/bean&gt;
 *         &lt;/constructor-arg&gt;
 *     &lt;/bean&gt;
 * </pre>
 * <p/>
 * <p>
 * If you're using iBATIS instead of Hibernate, use:
 * 
 * <pre>
 *     &lt;bean id="userManager" class="org.sbbs.service.impl.GenericManagerImpl"&gt;
 *         &lt;constructor-arg&gt;
 *             &lt;bean class="org.sbbs.dao.ibatis.GenericDaoiBatis"&gt;
 *                 &lt;constructor-arg value="org.sbbs.model.User"/&gt;
 *                 &lt;property name="dataSource" ref="dataSource"/&gt;
 *                 &lt;property name="sqlMapClient" ref="sqlMapClient"/&gt;
 *             &lt;/bean&gt;
 *         &lt;/constructor-arg&gt;
 *     &lt;/bean&gt;
 * </pre>
 * 
 * @param <T> a type variable
 * @param <PK> the primary key for that type
 * @author <a href="mailto:matt@raibledesigns.com">Matt Raible</a>
 */
/**
 * @author Administrator
 *
 * @param <T>
 * @param <PK>
 */
/**
 * @author Administrator
 *
 * @param <T>
 * @param <PK>
 */
public class BaseManagerImpl<T, PK extends Serializable>
    implements BaseManager<T, PK> {
    /**
     * Log variable for all child classes. Uses LogFactory.getLog(getClass()) from Commons Logging
     */
    protected final Log log = LogFactory.getLog( getClass() );

    /**
     * GenericDao instance, set by constructor of child classes
     */
    protected BaseDao<T, PK> dao;

    public BaseManagerImpl() {
    }

    public BaseManagerImpl( BaseDao<T, PK> genericDao ) {
        this.dao = genericDao;
    }

    /**
     * {@inheritDoc}
     */
    public List<T> getAll() {
        return dao.getAll();
    }

    /**
     * {@inheritDoc}
     */
    public T get( PK id ) {
        return dao.get( id );
    }

    /**
     * {@inheritDoc}
     */
    public boolean exists( PK id ) {
        return dao.exists( id );
    }

    /**
     * {@inheritDoc}
     */
    public T save( T object ) {
        return dao.save( object );
    }

    /**
     * {@inheritDoc}
     */
    public void remove( PK id ) {
        dao.remove( id );
    }

    protected PagedQueryDao hibernateCriteriaPagedQueryDao;

   
    public PagedQueryDao getHibernateCriteriaPagedQueryDao() {
        return hibernateCriteriaPagedQueryDao;
    }
    @Autowired
    public void setHibernateCriteriaPagedQueryDao( PagedQueryDao hibernateCriteriaPagedQueryDao ) {
        this.hibernateCriteriaPagedQueryDao = hibernateCriteriaPagedQueryDao;
    }

    @Override
    public IPager pagedQuery( Class clazz, IPager pager, SearchFilter searchFilter )
        throws Exception {
        return this.hibernateCriteriaPagedQueryDao.pagedQuery( clazz, pager, searchFilter );
    }

    @Override
    public IPager pagedQuery( Class clazz, IPager pager, SearchFilter searchFilter, CustomCriteria customCriteria )
        throws Exception {
        return this.hibernateCriteriaPagedQueryDao.pagedQuery( clazz, pager, searchFilter, customCriteria );
    }

}
